/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/remove-linked-list-elements
   @Language: C++
   @Datetime: 20-01-06 17:19
   */

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
	ListNode* removeElements(ListNode* head, int val) {
		ListNode H(-1), *p;
		for(H.next=head, p=&H; p->next;){
			if(p->next->val==val){
				head=p->next;
				p->next=head->next;
				delete head;
			}
			else p=p->next;
		}
		return H.next;
	}
};
